//
//  ChannelNum.m
//  DianNaoWang
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "PageCollectionViewCell.h"
#import "Header.h"

#import "CFNetworkFromAFNetworking.h"
#import "ChannelList.h"

#import "Type10HotTableViewCell.h"
#import "TopRotatoryShowPics.h"
#import "TopShowCollectionViewCell.h"
#import "MJRefresh.h"
#import "Type6ThreeImageCell.h"
#import "NormalPageTableViewCell.h"

@implementation PageCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _page = 1;
        [self creatTableView];
        self.topDataSourceArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 创建视图
- (void)creatTableView
{
    self.pageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    _pageTableView.delegate = self;
    _pageTableView.dataSource = self;
    _pageTableView.showsHorizontalScrollIndicator = NO;
    _pageTableView.showsVerticalScrollIndicator = NO;
    _pageTableView.tableFooterView = [[UIView alloc]init];
    _pageTableView.backgroundColor = BackgroundColor;
    
    [self.contentView addSubview:_pageTableView];
    
#pragma mark "菊花"
    _HUD = [MBProgressHUD showHUDAddedTo:self.contentView animated:YES];
    _HUD.backgroundColor = [UIColor clearColor];
    _HUD.labelText = @"正在刷新";
    _HUD.detailsLabelText = @"请耐心等待!";
    
#pragma mark MJ刷新
    _pageTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self getdataArray];
    }];
    _pageTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getdataArray];
    }];
    
    //夜间模式的监听者
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(colorChange) name:@"colorChange" object:nil];
}
#pragma mark - 夜间模式 监听到变化后采取的措施
- (void )colorChange
{
    self.contentView.backgroundColor = BackgroundColor;
    _pageTableView.backgroundColor = BackgroundColor;
}


#pragma mark - 值传入了,开始解析数据并赋值
-(void)setChannel:(ChannelNum *)channel
{
    if (_channel != channel) {
        _channel = channel;
    }
    self.dataSourceArray = [NSMutableArray array];
    _pageTableView.tableHeaderView = nil;
    [_pageTableView reloadData];//需要重载 否则tableView 位置 不刷新的
    _page = 1;
    _HUD.hidden = NO;
    [self getdataArray];
    

}

#pragma mark - tableView设置
#pragma mark 判断type类型 选择cell样式
//判断type类型 选择用哪种cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelList *channel = self.dataSourceArray[indexPath.row];
    ////NSLog(@"%@", channel.type);
    //判断type类型 显示不同cell
    if ([channel.type isEqualToString:@"10"] || [channel.type isEqualToString:@"2"]) {
        Type10HotTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell1) {
            cell1 = [[Type10HotTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
        }
        cell1.channelList = channel;
        return cell1;
    }else if ([channel.type isEqualToString:@"6"]) {
        Type6ThreeImageCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (!cell2) {
            cell2 = [[Type6ThreeImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        }
        cell2.channelList = channel;
        return cell2;
    }else
        //if ([channel.type isEqualToString:@"0"] || [channel.type isEqualToString:@"18"] || [channel.type isEqualToString:@"20"] || [channel.type isEqualToString:@"13"] || [channel.type isEqualToString:@"19"])
        {
        NormalPageTableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        if (!cell3) {
            cell3 = [[NormalPageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
        }
        cell3.channelList = channel;
        return cell3;
    }
//    else{
//        UITableViewCell *wrongCell = [tableView dequeueReusableCellWithIdentifier:@"wrongCell"];
//        if (!wrongCell) {
//            wrongCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"wrongCell"];
//        }
//        wrongCell.textLabel.text = @"貌似出现了点问题,请查找channel的type类型!";
//        return wrongCell;
//    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

#pragma mark 点击cell 跳转到web页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ////NSLog(@"%@", [_dataSourceArray[indexPath.row] Id]);
    [self.delegate paseValueAndPush:[_dataSourceArray[indexPath.row] Id] Title:[_dataSourceArray[indexPath.row] stitle]];
    //[self.delegate paseValueAndPush:[_dataSourceArray[indexPath.row] Id]];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelList *channel = self.dataSourceArray[indexPath.row];
    if ([channel.type isEqualToString:@"6"]) {
        return 165 * FitHeight;
    }else{
    return 90 * FitHeight;
    }
}

#pragma mark - 获取数据
- (void)getdataArray
{
    
    NSString *urlString = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=3.0&class_id=%@&page=%ld&retina=1&last_time=2015-09-28%@2009:50&vs=iph430&isReviewing=NO", _channel.Id, _page, @"%"];
    ////NSLog(@"%@", urlString);
    [CFNetworkFromAFNetworking askObjectByURLString:urlString BodyString:nil WithObjectBlock:^(id object) {
        NSArray *array = [object objectForKey:@"list"];
#pragma mark 如果第一页 下拉刷新都把原数组清空 再赋值
        if (_page == 1) {
            self.dataSourceArray = [NSMutableArray array];
            self.topDataSourceArray = [NSMutableArray array];
            
    //此处reload 会在你下拉刷新后浏览时突然跳到顶部 影响视觉效果
//            [_topCollectionView reloadData];//防止刷新时 滑动轮播图
//            [_pageTableView reloadData];//防止新数据未返回点击出现crash
        }
        for (NSDictionary *dic in array) {
            ChannelList *channelList = [[ChannelList alloc]init];
            [channelList setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:channelList];
        }

        if (_page == 1) {
            if ([object objectForKey:@"pics"]) {
                NSMutableArray *imaArray = [NSMutableArray array];
                NSMutableArray *titArray = [NSMutableArray array];
                NSArray *arr = [object objectForKey:@"pics"];
                for (NSDictionary *dict in arr) {
                    [imaArray addObject:[dict objectForKey:@"imgsrc"]];
                    [titArray addObject:[dict objectForKey:@"stitle"]];
                    TopRotatoryShowPics *topPic = [[TopRotatoryShowPics alloc]init];
                    [topPic setValuesForKeysWithDictionary:dict];
                    [self.topDataSourceArray addObject:topPic];
                }
                self.imagesURLStrings = [NSArray arrayWithArray:imaArray];
                self.titles = [NSArray arrayWithArray:titArray];
                //网络加载 --- 创建带标题的图片轮播器
                if (_imagesURLStrings.count != 0 && _titles != 0) {
                    self.cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 320, ScreenWidth, 200) imageURLStringsGroup:nil]; // 模拟网络延时情景
                    _cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
                    _cycleScrollView2.delegate = self;
                    self.cycleScrollView2.titlesGroup = _titles;
                    _cycleScrollView2.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
                    self.cycleScrollView2.placeholderImage = [UIImage imageNamed:@"123"];
                    
                    _pageTableView.tableHeaderView = _cycleScrollView2;
                    
                    //             --- 模拟加载延迟
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        self.cycleScrollView2.imageURLStringsGroup = [NSArray arrayWithArray:imaArray];
                    });
                }
            }
        }
        [_pageTableView reloadData];
        _HUD.hidden = YES;
        [_pageTableView.header endRefreshing];
        [_pageTableView.footer endRefreshing];
    }];
}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [self.delegate paseValueAndPush:[_topDataSourceArray[index] Id] Title:[_topDataSourceArray[index] stitle]];
}





@end
